package com.pureut.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.quality.domain.ProductDocument;
import com.pureut.quality.domain.ProductDocumentDetails;
import com.pureut.quality.domain.dto.ProductDocumentDetailsDto;
import com.pureut.quality.domain.dto.ProductDocumentDto;
import com.pureut.quality.domain.vo.ProductDocumentDetailsVo;
import com.pureut.quality.domain.vo.ProductDocumentVo;
import com.pureut.quality.domain.vo.SubmitVo;
import com.pureut.quality.mapper.ProductDocumentDetailsMapper;
import com.pureut.quality.mapper.ProductDocumentMapper;
import com.pureut.quality.service.ProductDocumentDetailsService;
import com.pureut.quality.service.ProductDocumentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/***
 * 评审管理
 * Author:C
 * Date:2023/01/19 17:15
 * @DESC
 */
@Service
public class ProductDocumentServiceImpl extends ServiceImpl<ProductDocumentMapper, ProductDocument> implements ProductDocumentService {

    /**
     * 评审管理 数据层
     */
    @Resource
    ProductDocumentMapper productDocumentMapper;

    /**
     * 评审管理明细 数据层
     */
    @Resource
    ProductDocumentDetailsMapper productDocumentDetailsMapper;

    /**
     * 评审管理明细
     */
    @Resource
    ProductDocumentDetailsService productDocumentDetailsService;

    /**
     * 查询评审管理列表
     * @param productDocumentVo
     * @return
     */
    @Override
    public List<ProductDocumentDto> listProductDocument(ProductDocumentVo productDocumentVo) {
        return productDocumentMapper.selectProductDocumentList(productDocumentVo);
    }

    /**
     * 查看评审管理
     * @param id
     * @return
     */
    @Override
    public ProductDocumentDto getInfo(Long id) {
        ProductDocumentDto info = productDocumentMapper.getInfo(id);
        List<ProductDocumentDetailsDto> productDocumentDetailsDtos = productDocumentMapper.selectDetailsList(info.getId());
        info.setProductDocumentDetailsVoList(productDocumentDetailsDtos);
        return info;
    }

    /**
     * 新增评审管理
     * @param productDocumentVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertProductDocument(ProductDocumentVo productDocumentVo) {

        ProductDocument productDocument = new ProductDocument();
        BeanUtils.copyProperties(productDocumentVo, productDocument);
        productDocument.setStatus("1");
        boolean save = save(productDocument);
        if (save){

            List<ProductDocumentDetails> list = new ArrayList<>();
            if (!productDocumentVo.getProductDocumentDetailsVoList().isEmpty()){
                List<ProductDocumentDetailsVo> detailList = productDocumentVo.getProductDocumentDetailsVoList();
                for (ProductDocumentDetailsVo productDocumentDetailsVo : detailList) {
                    ProductDocumentDetails productDocumentDetails = new ProductDocumentDetails();
                    BeanUtils.copyProperties(productDocumentDetailsVo, productDocumentDetails);
                    productDocumentDetails.setProductDocumentId(productDocument.getId());
                    list.add(productDocumentDetails);
                }
                productDocumentDetailsService.saveBatch(list);
            }
        } else {
            throw new GlobalException("新增评审管理失败!");
        }

        return save;
    }

    /**
     * 修改评审管理
     * @param ProductDocumentVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateProductDocument(ProductDocumentVo ProductDocumentVo) {
        ProductDocument byId = getById(ProductDocumentVo.getId());

        ProductDocument productDocument = new ProductDocument();
        BeanUtils.copyProperties(ProductDocumentVo, productDocument);
//        if (byId.getStatus().equals("4")){
//            productDocument.setStatus("1");
//        }
        productDocument.setId(ProductDocumentVo.getId());
        boolean b = updateById(productDocument);
        if (b){

            if (ProductDocumentVo.getProductDocumentDetailsVoList() != null){
                List<ProductDocumentDetails> productDocumentDetails = productDocumentDetailsMapper.selectList(new QueryWrapper<ProductDocumentDetails>()
                        .lambda().eq(ProductDocumentDetails::getProductDocumentId, ProductDocumentVo.getId()));
                if (!productDocumentDetails.isEmpty()){
                    productDocumentDetailsMapper.deleteBatchIds(productDocumentDetails);

                    List<ProductDocumentDetails> list = new ArrayList<>();
                    List<ProductDocumentDetailsVo> detailList = ProductDocumentVo.getProductDocumentDetailsVoList();
                    for (ProductDocumentDetailsVo productDocumentDetailsVo : detailList) {
                        ProductDocumentDetails entity = new ProductDocumentDetails();
                        BeanUtils.copyProperties(productDocumentDetailsVo, entity);
                        entity.setProductDocumentId(productDocument.getId());
                        list.add(entity);
                    }
                    productDocumentDetailsService.saveBatch(list);
                }
            }
        } else {
            throw new GlobalException("修改评审管理失败!");
        }
        return b;
    }

    /**
     * 删除评审管理
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeProductDocument(String ids) {
        boolean b = removeById(ids);
        if (b){
            List<ProductDocumentDetails> productDocumentDetails = productDocumentDetailsMapper.selectList(new QueryWrapper<ProductDocumentDetails>()
                    .lambda().eq(ProductDocumentDetails::getProductDocumentId, ids));
            if (!productDocumentDetails.isEmpty()){
                productDocumentDetailsMapper.deleteBatchIds(productDocumentDetails);
            }
        }
        return b;
    }

    /**
     * 关结
     * @param submitVo
     * @return
     */
    @Override
    public boolean closeProductDocument(SubmitVo submitVo) {
        List<ProductDocument> productDocuments = productDocumentMapper.selectList(new QueryWrapper<ProductDocument>()
                .lambda().in(ProductDocument::getId, submitVo.getIdList()));
        productDocuments.forEach(e -> e.setStatus("5"));
        return updateBatchById(productDocuments);
    }
}
